package com.aitesting.common;

import cn.hutool.core.util.StrUtil;
import com.aitesting.constant.AssertEnum;
import java.util.HashMap;
import java.util.Map;

public class Utils {

    /**
     * 合并两个变量
     * @param variables 第一个变量Map
     * @param variables_to_be_overridden 第二个变量Map
     * @return 合并后的变量Map
     */
    public static Map<String, Object> merge_variables(Map<String, Object> variables, Map<String, Object> variables_to_be_overridden){
        Map<String, Object> step_new_variables = new HashMap<>();
        for (Map.Entry<String, Object> entry : variables.entrySet()){
            String key1 = "$" + entry.getKey();
            String key2 = "${" + entry.getKey() + "}";
            if (key1.equals(entry.getValue()) || key2.equals(entry.getValue())){
                continue;
            }
            step_new_variables.put(entry.getKey(),entry.getValue());
        }
        variables_to_be_overridden.putAll(step_new_variables);
        return variables_to_be_overridden;
    }

    public static AssertEnum get_uniform_comparator(String comparator){
        if(StrUtil.containsAny(comparator,"eq", "equals", "equal", "=", "equalTo")){
            return AssertEnum.equal;
        }else if (StrUtil.containsAny(comparator, "lt", "less_than", "<", "lessThan")) {
            return AssertEnum.lessThan;
        }else if (StrUtil.containsAny(comparator, "le", "less_or_equals", "less_or_equal", "<=", "lessThanOrEqualTo")) {
            return AssertEnum.lessThanOrEqual;
        }else if (StrUtil.containsAny(comparator, "gt", "greater_than", ">", "greaterThan")) {
            return AssertEnum.greaterThan;
        }else if (StrUtil.containsAny(comparator, "ge", "greater_or_equals", "greater_or_equal", ">=", "greaterThanOrEqualTo")) {
            return AssertEnum.greaterThanOrEqual;
        }else if (StrUtil.containsAny(comparator, "in", "contain", "contains", "containsString")) {
            return AssertEnum.contains;
        }else if (StrUtil.containsAny(comparator, "len_eq", "length_equal", "hasLength")) {
            return AssertEnum.lengthEqual;
        }else if (StrUtil.containsAny(comparator, "ne", "not_equal", "not")) {
            return AssertEnum.notEqual;
        }else if (StrUtil.containsAny(comparator, "prefix", "start", "start_with", "startsWith")) {
            return AssertEnum.startsWith;
        }else if (StrUtil.containsAny(comparator, "suffix", "end", "end_with", "endsWith")) {
            return AssertEnum.endsWith;
        }else if (StrUtil.containsAny(comparator, "has_value", "hasValue")) {
            return AssertEnum.hasValue;
        }else if (StrUtil.containsAny(comparator, "has_item", "hasItem")) {
            return AssertEnum.hasItem;
        }else if (StrUtil.containsAny(comparator, "has_size","size", "hasSize")) {
            return AssertEnum.hasSize;
        }else if (StrUtil.containsAny(comparator, "hasItemOrContainsString")) {
            return AssertEnum.hasItemOrContainsString;
        }else {
            return AssertEnum.contains;
        }
    }
}
